package com.situ.springboot.controller;

import com.situ.springboot.pojo.Student;
import com.situ.springboot.service.IStudentService;
import com.situ.springboot.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

//@Controller
@RestController   // @Controller + @ResponseBody
public class StudentRestController {
    @Autowired
    private IStudentService studentService;

    //@RequestMapping(value = "/students", method = RequestMethod.GET)
    @GetMapping("/students")
    public Result selectAll() {
        System.out.println("StudentRestController.selectAll");
        List<Student> list = studentService.selectAll();
        return Result.ok(list);
    }

    // /students/1
    @GetMapping("/students/{id}")
    public Result selectById(@PathVariable("id") Integer id) {
        System.out.println("StudentRestController.selectById");
        Student student = studentService.selectById(id);
        return Result.ok(student);
    }

    @PostMapping("/students")
    public Result add(@RequestBody Student student) {
        System.out.println("StudentRestController.add");
        System.out.println(student);
        return Result.ok("添加成功");
    }

    // /students/deleteById?id=1
    // /students/1   Delete
    @DeleteMapping("/students/{id}")
    public Result deleteById(@PathVariable("id") Integer id) {
        System.out.println("StudentRestController.deleteById");
        System.out.println("id: " + id);
        return Result.ok("删除成功");
    }

    @PutMapping("/students")
    public Result update(Student student) {
        System.out.println("StudentRestController.update");
        System.out.println(student);
        return Result.ok("修改成功");
    }
}
